package com.visionet.school.app.web.repository.dynamic.impl;

import com.visionet.school.app.web.common.utils.PageUtils;
import com.visionet.school.app.web.common.web.WebContextFactory;
import com.visionet.school.app.web.entity.dynamic.vo.DynamicCommentVo;
import com.visionet.school.app.web.entity.vo.LoginAccountVo;
import com.visionet.school.app.web.repository.dynamic.DynamicCommentLikesRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hxpeng on 2017/6/16.
 */
public class DynamicCommentRepositoryImpl {

    private final static Logger logger = LoggerFactory.getLogger(DynamicCommentRepositoryImpl.class);

    @Autowired
    private DynamicCommentLikesRepository dynamicCommentLikesRepository;

    @Resource
    private JdbcTemplate jdbcTemplate;


    public Page<DynamicCommentVo> loadCommentPage(DynamicCommentVo dynamicCommentVo){
        final LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();

        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<>();
        sql.append("select tdc.id,tdc.member_id,tdc.member_name,tdc.member_avatar_path,tdc.dynamic_id,tdc.comment,");
        sql.append("tdc.parent_comment_id,tdc.to_member_id,tdc.to_member_name,to_member_avatar_path,tdc.likes,tdc.reply,");
        sql.append("tdc.create_date,tdc.create_by,tdc.update_by,tdc.update_date,tdc.is_deleted from t_dynamic_comment as tdc ");
        sql.append("where tdc.is_deleted = '0' ");

        if(StringUtils.isNotBlank(dynamicCommentVo.getParentCommentId())){
            sql.append("and tdc.parent_comment_id = ? ");
            params.add(dynamicCommentVo.getParentCommentId());
        } else {
            sql.append("and tdc.parent_comment_id is null ");
        }

        if (StringUtils.isNotBlank(dynamicCommentVo.getDynamicId())) {
            sql.append("and tdc.dynamic_id = ? ");
            params.add(dynamicCommentVo.getDynamicId());
        }
        sql.append("ORDER BY tdc.create_date DESC");
        PageRequest page = PageUtils.getPageRequestInfo(dynamicCommentVo.getPageInfo());
        if(page == null){
            page = new PageRequest(0, 10);
        }
        int currentPage = page.getPageNumber();
        int pageSize = page.getPageSize();
        int size = jdbcTemplate.queryForInt("select count(id) from ("+sql.toString()+") as c", params.toArray());
        List<DynamicCommentVo> list = jdbcTemplate.query("select t.* from ("+sql+") as t limit  "+ currentPage * pageSize+","+pageSize, params.toArray(), new RowMapper<DynamicCommentVo>() {
            @Override
            public DynamicCommentVo mapRow(ResultSet rs, int rowNum) throws SQLException {
                DynamicCommentVo entity = new DynamicCommentVo();
                String id = rs.getString("id");
                int commentLikesCount = dynamicCommentLikesRepository.commentHadLikes(id,loginAccountVo.getMemberId());
                if(commentLikesCount > 0){
                    entity.setHadLikes("1");
                }else{
                    entity.setHadLikes("0");
                }
                entity.setId(id);
                entity.setMemberId(rs.getString("member_id"));
                entity.setMemberName(rs.getString("member_name"));
                entity.setMemberAvatarPath(rs.getString("member_avatar_path"));
                entity.setDynamicId(rs.getString("dynamic_id"));
                entity.setComment(rs.getString("comment"));
                entity.setParentCommentId(rs.getString("parent_comment_id"));
                entity.setToMemberId(rs.getString("to_member_id"));
                entity.setToMemberName(rs.getString("to_member_name"));
                entity.setToMemberAvatarPath(rs.getString("to_member_avatar_path"));
                entity.setLikes(rs.getInt("likes"));
                entity.setReply(rs.getInt("reply"));
                entity.setCreateDate(rs.getTimestamp("create_date"));
                return entity;
            }
        });
        Page<DynamicCommentVo> cmProductGroupTourPage = new PageImpl<>(list, page, size);
        return cmProductGroupTourPage;
    }

}
